/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.businessLayer;

import mnstudent.dataacesslayer.StaffDAL;
import mnstudent.datatranferobject.StaffDTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author 11520_000
 */
public class StaffBLL {
    
    private StaffDAL m_StaffDAL;
    
    public StaffBLL()
    {
        m_StaffDAL = new StaffDAL();
    }
    
    //<editor-fold defaultstate="collapsed" desc="Method">
     /**
     * Ham lay tat ca cac Nhan vien
     *
     * @return ResultSet
     */
    public ResultSet getAllStaff(){
       
        return m_StaffDAL.getAllStaff();
    }
    
     /**
     * Ham lay khoa theo ma khoa
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getStaffById(String _id)
    {
       return m_StaffDAL.getStaffById(_id);
    }
    
    public ResultSet getStaffByUserNameAndPasswordFromDatabase(String _userName, String _passWord)
    {
       return m_StaffDAL.getStaffByUserNameAndPasswordFromDatabase(_userName, _passWord);
    }
    
    public ArrayList<StaffDTO> getAllStaffToArray()
    {
        ArrayList<StaffDTO> arrStaff= new ArrayList<>();
        try {
            ResultSet rs = getAllStaff();
            while(rs.next())
            {
                StaffDTO staff = new StaffDTO(rs.getNString(1), rs.getNString(2), rs.getNString(3),
                        rs.getDate(4), rs.getNString(5), rs.getNString(6), 
                        rs.getNString(7), rs.getNString(8), rs.getNString(9) );
                arrStaff.add(staff);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return arrStaff;
    }
    
     /**
     * ham them moi khoa xuong database
     * @param _staff
     * @return true: success, false: fail
     */
    public boolean insertStaffToDatabase(StaffDTO _staff)
    {
        return m_StaffDAL.insertStaffToDatabase(_staff);
    }
    
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _staff
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateStaffToDatabase(StaffDTO _staff)
    {
        return m_StaffDAL.updateStaffToDatabase(_staff);
    }
        
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _staff
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateStaffNoPasswordToDatabase(StaffDTO _staff)
    {
        return m_StaffDAL.updateStaffNoPasswordToDatabase(_staff);
    }
    /**
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _staff
     * @return tue: thanh cong, false: that bai
     */
    public boolean updatePasswordOfStaffToDatabase(String _idStaff, String _newPassword)
    {
        return m_StaffDAL.updatePasswordOfStaffToDatabase(_idStaff, _newPassword);
    }
    
            
    /**
     * Ham xoa mot khoa ra khoi database
     * @param _staff
     * @return 
     */
     public boolean deleteStaffFromDatabase(StaffDTO _staff)
    {
         return m_StaffDAL.deleteStaffFromDatabase(_staff);
    }
     
    public boolean checkInputIsInvalid(JXTable _jtb, String _input)
    {
        for(int i = 0; i<_jtb.getRowCount(); i++)
        {
            if(_jtb.getSelectedRow() != i){

                if(_input.replaceAll("\\s+", "").toLowerCase().equals(_jtb.getValueAt(i, 1).toString().replaceAll("\\s+", "").toLowerCase())){
                    return false;
                }
            }
        }
          return true;
     }
    //</editor-fold>
}
